package chinaums.common.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;



/**
 * <p>Title:HttpUtils</p>
 * <p>Description:http请求包</p>
 * @author RenLei
 * 2017年7月26日
 */
public class HttpUtils {
	
	
	
	public static  String post(String url, String jsonIn){
		String jsonStr = null;
		 try {
			 	DefaultHttpClient httpClient = new DefaultHttpClient();
//			 	DefaultHttpClient httpClient = wrapClient(new DefaultHttpClient());
				HttpPost httpPost = new HttpPost(url);
				httpPost.addHeader(HTTP.CONTENT_TYPE,
						"application/json");
				httpPost.addHeader("CONNECTION", HTTP.CONN_CLOSE);
				//String str=new String(jsonIn,"utf-8");
				//jsonIn=new String(jsonIn.getBytes("gbk"),"utf-8");
				StringEntity se = new StringEntity(jsonIn,"UTF-8");
				httpPost.setEntity(se);
				HttpResponse response = httpClient.execute(httpPost);
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				jsonStr = getStringFromInputStream(content);
				//System.out.println(jsonStr+"null");
				content.close();
				httpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return jsonStr.toString();
	}
	
	private static String getStringFromInputStream(InputStream content) {
	     StringBuilder responseStrBuilder = new StringBuilder();
		 try {
			 BufferedReader streamReader = new BufferedReader(new InputStreamReader(content, "UTF-8"));
		     String inputStr;
			 while ((inputStr = streamReader.readLine()) != null){
			    responseStrBuilder.append(inputStr);
			}
			 streamReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		 
		 String respString = responseStrBuilder.toString();
//			// System.out.println(respString);
			 return respString;
		/* JSONObject jsonObject = JSONObject.fromObject(responseStrBuilder.toString());
			return jsonObject.toString();*/

	}

	public  DefaultHttpClient wrapClient(HttpClient base) {
		try {
			SSLContext ctx = SSLContext.getInstance("TLS");
			X509TrustManager tm = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] chain,
						String authType) throws CertificateException {
				}

				public void checkServerTrusted(X509Certificate[] chain,
						String authType) throws CertificateException {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}

				public boolean isClientTrusted(X509Certificate[] arg0) {
					return false;
				}

				public boolean isServerTrusted(X509Certificate[] arg0) {
					return true;
				}
			};
			ctx.init(null, new TrustManager[] { tm }, null);
			SSLSocketFactory ssf = new SSLSocketFactory(ctx); 
	ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			ClientConnectionManager ccm = base.getConnectionManager();
			SchemeRegistry sr = ccm.getSchemeRegistry();
			sr.register(new Scheme("https", ssf, 8080));
			return new DefaultHttpClient(ccm, base.getParams());
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}


 


	


